package TetrisFlash.Objects.TetrisBoard 
{
	import TetrisFlash.Screen.Screen;
	import TetrisFlash.GlobalData.Global;
	import TetrisFlash.Objects.Tetrominoes.ITetromino;
	import starling.display.QuadBatch;
	import starling.display.Image;
	import TetrisFlash.GameLoadData.AssetsTexture;
	/**
	 * ...
	 * @author DANTRUONG
	 */
	public class TetrisBoard 
	{
		//number column of board
		public var column:int;
		
		//number row of board
		public var row:int;	
		
		//Array of board
		public var boardArray:Array;
		
		//screen dislay this Board
		public var screen:Screen;
		
		//QuadBatch store and draw Board
		public var boardQuadBatch:QuadBatch;
		
		//Image element of board
		public var boardImage:Image;
		
		public function TetrisBoard(_screen: Screen) 
		{
			//init row + column
			column = Global.GAMEBOARD_COLUMN;
			row = Global.GAMEBOARD_ROW;
			screen = _screen;
			
			//Create board empty
			boardArray = new Array();
			for (var i:int = 0; i < column; i++) {
				boardArray[i] = new Array();
			}
			for (var k:int = 0; k < column; k++) {
				for (var j:int = 0; j < row; j++) {
					boardArray[k][j] = 0;
				}
			}
			
			//Create quadbatch and add image
			boardQuadBatch = new QuadBatch();
			boardImage = new Image(AssetsTexture.getTetrisAtlas().getTexture("Brick_0"));
		}
		
		//Add a matrix tetromino to matrix board
		public function addTetromino(iTetromino: ITetromino): void {
			for (var i:int = 0; i < iTetromino.column; i++) {
				for (var j:int = 0; j < iTetromino.row; j++) {
					if (iTetromino.tetrominoArray[i][j]) {
						boardArray[iTetromino.posX + i][iTetromino.posY + j] = 
						iTetromino.tetrominoArray[i][j];
					}
				}
			}
			//if add done then release the tetro which has been added
			iTetromino.release();
		}
		
		//Check board is full - to over game
		public function isFull():Boolean {
			for (var i:int = 0; i < column; i++) {
				if (boardArray[i][1]) {
					return true;
				}
			}
			return false;
		}
		
		//Check a line is full
		public function isFullLine(line:int):Boolean {
			for (var i:int = 0; i < column; i++) {
				if (!boardArray[i][line]) {
					return false;
				}
			}
			return true;
		}
		
		//Clear lines be full - scroll their
		public function clearLines():void {
			for (var j:int = 2; j < row; ) {
				if (isFullLine(j)) {
					//Replace until to j = 2
					while(j>=2) {
						for (var i:int = 0; i < column; i++) {
							boardArray[i][j] = boardArray[i][j - 1];
						}
						j--;
					}
				}else {
					j++;
				}
			}
		}
		
		//Count number of full lines
		public function countFullLines():int {
			var count:int = 0;
			for (var j:int = 2; j < row; j++) {
				if (isFullLine(j)) {
					count++;
				}
			}
			return count;
		}
		
		//Draw board
		public function drawBoard():void {
			/*boardQuadBatch = new QuadBatch();
			screen.addChild(boardQuadBatch);*/
			
			//add image to quadbatch
			for (var i: int = 0; i < column; i++) {
					for (var j: int = 2; j < row; j++) {
							if (boardArray[i][j]) {
								boardImage.x = Global.OFFSET_TOBOARD_X + i * Global.CELL_WIDTH;
								boardImage.y = Global.OFFSET_TOBOARD_Y + (j - 2) * Global.CELL_WIDTH;
								boardQuadBatch.addImage(boardImage);
							}
					}
			}
		}
		
		//Draw board with draw game loop
		public function updateDrawBoard():void {
			/*if(screen.contains(boardQuadBatch)){
				screen.removeChild(boardQuadBatch);
			}
			boardQuadBatch.dispose();*/
			
			//if screen not contain quadbatch then add quadbatch
			if(!screen.contains(boardQuadBatch)){
				screen.addChild(boardQuadBatch);
			}
			
			//reset all data of board but not dispose
			boardQuadBatch.reset();
			
			//draw board
			drawBoard();
		}
		
		public function release():void {
			boardArray.length = 0;
			if (screen.contains(boardQuadBatch)) {
				screen.removeChild(boardQuadBatch);
			}
			
			boardImage.dispose();
			boardQuadBatch.dispose();
		}
		
	}
}